from Common import sql_content,Fession_mini
from ReckonJob.ReckonJobControl import changeKey

class _SelectJob(object):
    def __init__(self):
        self.sql_content = sql_content.sql_content()
        self.allJobNow = []
        self.all_word = []
        #self.#前台传递过来的是1234，需要在这里进行转换，或者单独写一个文件

    def SelectJob_main(self,direct = 0):

        print('selectJob',Fession_mini.getFessionMini('selectJob'))
        #selectJob {'education': '1-2-3-4-5-'}
        if direct == 1:
            self.all_word = self.policy_sql_type()
            self.Select_sql(Fession_mini.getFessionMini('directSearch'),1)
        else:
            changeKey.changeKey(Fession_mini.getFessionMini('selectJob'))
            allConditon = Fession_mini.getFessionMini('selectJob')
            self.Select_sql(allConditon)

        return self.allJobNow,int(len(self.allJobNow))
        # self.Select_sql(condition)


    # def normalization(self):
    #     nowCondition = Fession_mini.getFessionMini('selectJob')
    #     for i in len(nowCondition):
    #         pass
    #连接51数据库
    def Select_sql(self,condition,direct=0):
        con = self.sql_content.conten()
        sqlStr = "select * from data_51job_she WHERE "
        if direct == 1:
            sqlStr="SELECT * from data_51job_she WHERE CONCAT("
            for i in self.all_word:
                sqlStr += "IFNULL(`" + i + "`, ''),"

            sqlStr = sqlStr[:len(sqlStr) - 1]
            sqlStr += ") LIKE '%" + condition + "%';"
        else:
            for i in condition:
                for j in condition[i]:
                    sqlStr += i + " like '%" + j +"%' or "
            sqlStr =sqlStr[:len(sqlStr)-3]
        print(sqlStr)
        sql_statement = sqlStr
        cursor = con.cursor()
        try:
            cursor.execute(sql_statement)
            rs = cursor.fetchall()
            seq = 0
            for row in rs:
                list_row = list(row)
                list_row[6] = list_row[6][:150] + '....'
                list_row[7] = list_row[7][:150] + '....'
                list_row.append(seq)
                seq += 1
                #print(list_row)
                self.allJobNow.append(list_row)
        except Exception as e:
            print(e)
            con.rollback()
        cursor.close()
        con.close()

        #print(self.allJobNow)
    #检查数据库有几种类型type
    def policy_sql_type(self):
        list_data = []
        con = self.sql_content.conten()
        sql_policy = "select COLUMN_NAME from information_schema.COLUMNS where table_name = 'data_51job_she';"
        cursor = con.cursor()
        try:
            cursor.execute(sql_policy)
            rs = cursor.fetchall()
            for r in rs:
               if r not in list_data:
                   list_data.append(r[0])
            print(list_data)
        except Exception as e:
            print(e)
            con.rollback()
        cursor.close()
        con.close()

        return list_data
#测试口
if __name__ == "__main__":
    dict = {'Academic': ['初中', '中专', '大专', '本科', '本科', '硕士']}
    _SelectJob().policy_sql_type()
